#!/usr/bin/python
# -*- coding: utf-8 -*-

import urllib2 as urllib
from BeautifulSoup import BeautifulSoup as bs
import sys
import os

HEADER = '\033[1;37;41m'
HEADER2 = '\033[37;41m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
NUMBER = '\033[1;33m'


class wordTranslate:

    base_url = "http://m.seslisozluk.com/?word="
    translations = ()
    trans_dict = {
            "dr_tr_en" : "English",
            "dr_en_tr" : "Turkish",
            "dr_tr_es" : "Spanish",
            "dr_es_tr" : "Turkish",
            "dr_en_es" : "Spanish",
            "dr_es_en" : "English",
            "dr_en_en" : "English",
            "dr_es_es" : "Spanish",
            "dr_tr_tr" : "Turkish",
    }

    def __init__(self, query):
        self.query = query
        self.url = self.base_url + self.query
        self.html = urllib.urlopen(self.url).read()
        self.soup = bs(self.html)

        print "\n"
        for id, title in self.trans_dict.iteritems():
            obj = self.soup.find('tbody', attrs={'id':id})
            if obj != None:
                print self.getTemplate(id, title, obj)

    def getTemplate(self, id, title, soup):
        id = " ".join(id.split("-"))[3:5]
        soup = soup.contents
        list_range = range(1, len(soup))[::2]
        template = ENDC + HEADER2 + id + ENDC + HEADER + " - " + title + ENDC + "\n" + ENDC
        content = ""
        y = 1

        for i in list_range:
            item = NUMBER + str(y) + ENDC + ". "
            item = item + soup[i].strip()
            item = item + ENDC + " "
            content = content + item + ENDC
            y = y+1

        template = template + content
        template = template + "\n"
        return template

a = wordTranslate(sys.argv[1])
